﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarPark.MainModel
{
    public partial class DateAndTimePicker : UserControl
    {
        private DateTime dateAndtime;
        public event DateOrTimeChangedHandler DateOrTimeChanged;
        public DateTime DateAndTime
        {
            get 
            {
                dateAndtime = dateTimePicker1.Value.Add(new TimeSpan(Convert.ToInt16( hour.SelectedItem),
                                                                     Convert.ToInt16( mimute.SelectedItem),
                                                                     0)
                                                        );
                return dateAndtime;
            }
        }

        private bool isenble;
        public bool IsEnable
        {
            get { return isenble; }
            set { 
                    isenble = value;
                    check.Checked = isenble;
            }
        }


        public DateAndTimePicker()
        {
            InitializeComponent();

            for (int i = 0; i < 24; i++)
                hour.Items.Add(string.Format(i.ToString()));
            for (int i = 0; i < 60; i++)
                mimute.Items.Add(string.Format(i.ToString()));
            dateTimePicker1.Value = DateTime.Today;
            hour.SelectedIndex = 0;
            mimute.SelectedIndex = 0;

            this.dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);

            IsEnable = true;

            this.hour.SelectedIndexChanged += new EventHandler(hour_SelectedIndexChanged);
            this.mimute.SelectedIndexChanged += new EventHandler(mimute_SelectedIndexChanged);
            this.DateOrTimeChanged += new DateOrTimeChangedHandler(DateAndTimePicker_DateOrTimeChanged);
        }

        protected virtual void DateAndTimePicker_DateOrTimeChanged(object sender, DateOrTimeChangedArg e)
        {
        }

        void mimute_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateOrTimeChanged(this, new DateOrTimeChangedArg(DateAndTime));
        }

        void hour_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateOrTimeChanged(this, new DateOrTimeChangedArg(DateAndTime));
        }

        void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateOrTimeChanged(this, new DateOrTimeChangedArg(DateAndTime));
        }

        private void check_CheckedChanged(object sender, EventArgs e)
        {
            this.dateTimePicker1.Enabled = this.check.Checked;
            this.hour.Enabled = this.check.Checked;
            this.mimute.Enabled = this.check.Checked;
            isenble = this.check.Checked;
        }
    }

    public delegate void DateOrTimeChangedHandler(object sender , DateOrTimeChangedArg e);

    public class DateOrTimeChangedArg : EventArgs
    {
        private DateTime datetime;
        public DateTime DateTime { get { return datetime; } }
        public DateOrTimeChangedArg(DateTime dt)
        {
            datetime = dt;
        }
    }

}
